#!/bin/bash
# ----------------------------------------------------------------
#   5-RunFEAT.sh: Run the standard fMRI analysis pipeline
#
#	Usage:
#		5-RunFEAT.sh rawDataSrc projectDir subjID
#   initializes the Adaptation fMRI experiment. This experiment 
#   effect of stimulus history on the current evoked response,
#   on serial position -1 to -N are manipulated in a 
#
#
#   Inputs
#   -------
#   [protocolListFileName]: path to a file that should contain a
#   	description of the protocols to be used. If nothing is
#   	the file ./Config/ProtocolList.cfg will be used
#
#
#   Outputs
#   -------
#   ./Data/[expType]/[protocol]/[subjID]/[subjID]-[protocol]-
#  	this file will store all experimental info, experiment
#   	parameters, stimuli used, and data collected
#
#   Examples
#   -------
#    i) AdaptID_v1_1('./protocols.txt')
#   ii) AdaptID_v1_1
#
# ----------------------------------------------------------------
# ``Redistribution and use, with or without modification, are
#	permitted provided that the list of contributors below is
#	preserved.``
#
#   Original code by Marcelo G Mattar (09/04/2012)
#	mattar@sas.upenn.edu
# ----------------------------------------------------------------


# --- Check input command ---
if [ $# -ne 4 ]; then
cat << EOF
USAGE: `basename $0` dataDir analysisDir subjectID directoryStructureFile
EOF
	exit 1
fi

# --- Read input parameters ---
dataDir=$1
analysisDir=$2
subjectID=$3
directoryStructureFile=$4

# --- Set up some important variables ---
OCD=$PWD
BASEDIR=$(dirname $0)
TEMPLATEFILE=$BASEDIR/FEAT_MainExp.fsf

# --- Set up sequence filenames ---
sequenceName[1]=Face1
sequenceName[2]=Face2
sequenceName[3]=Face3
sequenceName[4]=Face4
sequenceName[5]=Color1
sequenceName[6]=Color2
sequenceName[7]=Color3
sequenceName[8]=Color4
countSeq=1

# --- Read directory structure from external file
. $directoryStructureFile




# Loop through each scan and run the first level analysis
for (( i = 1 ; i <= ${#scanName[@]} ; i++ )) do
	if [ "${scanType[$i]}" == "ShortTermAdapt" ]; then
		echo Working on ${scanType[$i]} - ${scanName[$i]}
		
		# --- Define the name of the temporary design file ---
		DESIGNFILE=`echo $BASEDIR/FEAT_${subjectID}_${scanName[$i]}.fsf`
		
		# --- Create a temporary design file ---
		cp $TEMPLATEFILE $DESIGNFILE
		
		
		# -----------------------------------------------------
		# 1) --- BASIC PARAMETERS ---
		# -----------------------------------------------------

		DATAFILE=$dataDir/$subjectID/${scanType[$i]}/${scanName[$i]} #Data file (/jet/mattar/AdaptID/Data/R091412V/MainExp/Face1)
		OUTPUTDIR=$analysisDir/$subjectID/${scanName[$i]}.feat #Output directory (/jet/mattar/AdaptID/Analyses/R091412V/Face1.feat)
		NUMVOLUMES=120 #Total volumes (120)
		

		# -----------------------------------------------------
		# 2) --- PRESTATS PARAMETERS ---
		# -----------------------------------------------------
		
		MOTION_CORR_YES_NO=1 #Motion correction (1)
		HIGHPASS_CUTOFF=100 #High pass filter cutoff (ms) (50)
		
		B0_YES_NO=0 #B0 fieldmap unwarping? (1)
		EPI_DWELLTIME=0.8 #EPI dwell time (ms) (0.5)
		EPI_TE=30 #EPI TE (ms) (30)
		B0_FIELDMAP=$dataDir/$subjectID/B0map/fieldmap/B0fieldmap_rads_brain #B0 unwarp input image for analysis 1 (/jet/mattar/AdaptID/Data/R091412V/B0map/fieldmap/B0fieldmap_rads_brain)
		B0_MAG=$dataDir/$subjectID/B0map/B0mag #B0 unwarp mag input image for analysis 1 (/jet/mattar/AdaptID/Data/R091412V/B0map/B0mag)
		
		SLICETIMING_CORR_YES_NO=0 #Slice timing correction (0-5, see template file)
		SLICETIMING_FILE= #Slice timings file (/jet/mattar/AdaptID/Scripts/sliceSeq.txt, or (blank))
		
		BET_YES_NO=1 #BET brain extraction - (1)
		SMOOTHING=5 #Spatial smoothing FWHM (mm) - (5)


		# -----------------------------------------------------
		# 3) --- STATS PARAMETERS ---
		# -----------------------------------------------------

		NUMBER_EVS=5 #Number of EVs (original) (5)
		NUMBER_EVS_REAL=10 #Number of EVs (real) (10)
		NUMBER_CONTRASTS=3 #Number of contrasts (3)
		
		COVNAME1=mainEffect #EV 1 title (MainEffect)
		COVTYPE1=3 #Basic waveform shape (0-4, see template file)
		COVFILE1=/jet/mattar/AdaptID/Scripts/covariates/main/${sequenceName[$countSeq]}_mainEffect.txt #Custom EV file (/jet/mattar/AdaptID/Scripts/covariates/main/5targetsA_mainEffect.txt)
		COVDERIVATIVE1=1 #Add temporal derivative (1)
		
		COVNAME2=targetTrials #EV 2 title (targetTrials)
		COVTYPE2=3 #Basic waveform shape (0-4, see template file)
		COVFILE2=/jet/mattar/AdaptID/Scripts/covariates/main/${sequenceName[$countSeq]}_targetTrials.txt #Custom EV file (/jet/mattar/AdaptID/Scripts/covariates/main/5targetsA_targetTrials.txt)
		COVDERIVATIVE2=1 #Add temporal derivative (1)

		COVNAME3=afterBlanks #EV 3 title (afterBlanks)
		COVTYPE3=3 #Basic waveform shape (0-4, see template file)
		COVFILE3=/jet/mattar/AdaptID/Scripts/covariates/main/${sequenceName[$countSeq]}_afterBlanks.txt #Custom EV file (/jet/mattar/AdaptID/Scripts/covariates/main/5targetsA_afterBlanks.txt)
		COVDERIVATIVE3=1 #Add temporal derivative (1)
		
		COVNAME4=afterTargets #EV 4 title (afterTargets)
		COVTYPE4=3 #Basic waveform shape (0-4, see template file)
		COVFILE4=/jet/mattar/AdaptID/Scripts/covariates/main/${sequenceName[$countSeq]}_afterTargets.txt #Custom EV file (/jet/mattar/AdaptID/Scripts/covariates/main/5targetsA_afterTargets.txt)
		COVDERIVATIVE4=1 #Add temporal derivative (1)
		
		COVNAME5=carryOver1 #EV 5 title (carryOver1)
		COVTYPE5=3 #Basic waveform shape (0-4, see template file)
		COVFILE5=/jet/mattar/AdaptID/Scripts/covariates/main/${sequenceName[$countSeq]}_carryOver1.txt #Custom EV file (/jet/mattar/AdaptID/Scripts/covariates/main/5targetsA_carryOver1.txt)
		COVDERIVATIVE5=1 #Add temporal derivative (1)
		
		# ATTENTION! CONTRASTS SHOULD BE EDITED DIRECTLY ON THE TEMPLATE FILE!


		# -----------------------------------------------------
		# 4) --- POSTSTATS PARAMETERS ---
		# -----------------------------------------------------

		THRESHOLDING=0 #Thresholding (0-3, see template file)
		P_THRESHOLD=0.05 #P threshold (0.05)
		Z_THRESHOLD=2.3 #Z threshold (2.3)
		

		# -----------------------------------------------------
		# 5) --- REGISTRATION PARAMETERS ---
		# -----------------------------------------------------

		REGISTRATION_1=1 #Registration to initial structural (1)
		REGFILE_1=$dataDir/$subjectID/Anatomy/BET/EPI_singlerep_brain_0.4 #Session structural image for analysis 1 (/jet/mattar/AdaptID/Data/R091412V/Anatomy/BET/EPI_singlerep_brain_0.4)
		SEARCHSPACE_1=90 #Search space for registration to initial structural (90)
		DOF_1=3	#Degrees of Freedom for registration to initial structural (3)
		
		REGISTRATION_2=1 #Registration to main structural (1)
		REGFILE_2=$dataDir/$subjectID/Anatomy/BET/EPI_wholebrain_brain_0.4 #Subject structural image for analysis 1 (/jet/mattar/AdaptID/Data/R091412V/Anatomy/BET/EPI_wholebrain_brain_0.4)
		SEARCHSPACE_2=90 #Search space for registration to main structural (90)
		DOF_2=3	#Degrees of Freedom for registration to main structural (3)

		REGISTRATION_3=1 #Registration to standard image? (1)
		REGFILE_3=$dataDir/$subjectID/Anatomy/BET/MPRAGE_brain_0.4 #Standard image (/jet/mattar/AdaptID/Data/R091412V/Anatomy/BET/MPRAGE_brain_0.4)
		SEARCHSPACE_3=90 #Search space for registration to standard space (90)
		DOF_3=7	#Degrees of Freedom for registration to standard space (7)
		
		
		# -----------------------------------------------------
		# 6) --- PERFORM SUBSTITUTIONS ON DESIGN FILE ---
		# -----------------------------------------------------
		
		sed -i 's|##DATAFILE##|'$DATAFILE'|' $DESIGNFILE
		sed -i 's|##OUTPUTDIR##|'$OUTPUTDIR'|' $DESIGNFILE
		sed -i 's|##NUMVOLUMES##|'$NUMVOLUMES'|' $DESIGNFILE

		sed -i 's|##MOTION_CORR_YES_NO##|'$MOTION_CORR_YES_NO'|' $DESIGNFILE
		sed -i 's|##HIGHPASS_CUTOFF##|'$HIGHPASS_CUTOFF'|' $DESIGNFILE
		sed -i 's|##B0_YES_NO##|'$B0_YES_NO'|' $DESIGNFILE
		sed -i 's|##EPI_DWELLTIME##|'$EPI_DWELLTIME'|' $DESIGNFILE
		sed -i 's|##EPI_TE##|'$EPI_TE'|' $DESIGNFILE
		sed -i 's|##B0_FIELDMAP##|'$B0_FIELDMAP'|' $DESIGNFILE
		sed -i 's|##B0_MAG##|'$B0_MAG'|' $DESIGNFILE
		sed -i 's|##SLICETIMING_CORR_YES_NO##|'$SLICETIMING_CORR_YES_NO'|' $DESIGNFILE
		sed -i 's|##SLICETIMING_FILE##|'$SLICETIMING_FILE'|' $DESIGNFILE
		sed -i 's|##BET_YES_NO##|'$BET_YES_NO'|' $DESIGNFILE
		sed -i 's|##SMOOTHING##|'$SMOOTHING'|' $DESIGNFILE
		
		sed -i 's|##NUMBER_EVS##|'$NUMBER_EVS'|' $DESIGNFILE
		sed -i 's|##NUMBER_EVS_REAL##|'$NUMBER_EVS_REAL'|' $DESIGNFILE
		sed -i 's|##NUMBER_CONTRASTS##|'$NUMBER_CONTRASTS'|' $DESIGNFILE
		sed -i 's|##COVNAME1##|'$COVNAME1'|' $DESIGNFILE
		sed -i 's|##COVTYPE1##|'$COVTYPE1'|' $DESIGNFILE
		sed -i 's|##COVFILE1##|'$COVFILE1'|' $DESIGNFILE
		sed -i 's|##COVDERIVATIVE1##|'$COVDERIVATIVE1'|' $DESIGNFILE
		sed -i 's|##COVNAME2##|'$COVNAME2'|' $DESIGNFILE
		sed -i 's|##COVTYPE2##|'$COVTYPE2'|' $DESIGNFILE
		sed -i 's|##COVFILE2##|'$COVFILE2'|' $DESIGNFILE
		sed -i 's|##COVDERIVATIVE2##|'$COVDERIVATIVE2'|' $DESIGNFILE
		sed -i 's|##COVNAME3##|'$COVNAME3'|' $DESIGNFILE
		sed -i 's|##COVTYPE3##|'$COVTYPE3'|' $DESIGNFILE
		sed -i 's|##COVFILE3##|'$COVFILE3'|' $DESIGNFILE
		sed -i 's|##COVDERIVATIVE3##|'$COVDERIVATIVE3'|' $DESIGNFILE
		sed -i 's|##COVNAME4##|'$COVNAME4'|' $DESIGNFILE
		sed -i 's|##COVTYPE4##|'$COVTYPE4'|' $DESIGNFILE
		sed -i 's|##COVFILE4##|'$COVFILE4'|' $DESIGNFILE
		sed -i 's|##COVDERIVATIVE4##|'$COVDERIVATIVE4'|' $DESIGNFILE
		sed -i 's|##COVNAME5##|'$COVNAME5'|' $DESIGNFILE
		sed -i 's|##COVTYPE5##|'$COVTYPE5'|' $DESIGNFILE
		sed -i 's|##COVFILE5##|'$COVFILE5'|' $DESIGNFILE
		sed -i 's|##COVDERIVATIVE5##|'$COVDERIVATIVE5'|' $DESIGNFILE
		
		sed -i 's|##THRESHOLDING##|'$THRESHOLDING'|' $DESIGNFILE
		sed -i 's|##P_THRESHOLD##|'$P_THRESHOLD'|' $DESIGNFILE
		sed -i 's|##Z_THRESHOLD##|'$Z_THRESHOLD'|' $DESIGNFILE
		
		sed -i 's|##REGISTRATION_1##|'$REGISTRATION_1'|' $DESIGNFILE
		sed -i 's|##REGFILE_1##|'$REGFILE_1'|' $DESIGNFILE
		sed -i 's|##SEARCHSPACE_1##|'$SEARCHSPACE_1'|' $DESIGNFILE
		sed -i 's|##DOF_1##|'$DOF_1'|' $DESIGNFILE
		sed -i 's|##REGISTRATION_2##|'$REGISTRATION_2'|' $DESIGNFILE
		sed -i 's|##REGFILE_2##|'$REGFILE_2'|' $DESIGNFILE
		sed -i 's|##SEARCHSPACE_2##|'$SEARCHSPACE_2'|' $DESIGNFILE
		sed -i 's|##DOF_2##|'$DOF_2'|' $DESIGNFILE
		sed -i 's|##REGISTRATION_3##|'$REGISTRATION_3'|' $DESIGNFILE
		sed -i 's|##REGFILE_3##|'$REGFILE_3'|' $DESIGNFILE
		sed -i 's|##SEARCHSPACE_3##|'$SEARCHSPACE_3'|' $DESIGNFILE
		sed -i 's|##DOF_3##|'$DOF_3'|' $DESIGNFILE
		
		# -----------------------------------------------------
		# 7) --- RUN FEAT ---
		# -----------------------------------------------------

		feat $DESIGNFILE
		
		# -----------------------------------------------------
		# 8) --- REMOVE DESIGN FILE ---
		# -----------------------------------------------------

		rm -f $DESIGNFILE
		
		((countSeq++))
		
	fi
done

cd $OCD

exit 0